// src/main/java/com/cupk/controllers/ProjectController.java (最终正确版 - 解决重复定义)

package com.cupk.controllers;

import com.cupk.dtos.CreateProjectRequest;
import com.cupk.dtos.MessageResponse;
import com.cupk.dtos.PageDto;
import com.cupk.dtos.ProjectDto;
import com.cupk.dtos.UpdateProjectRequest; // 确保已导入
import com.cupk.services.ProjectService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List; // 确保已导入

@RestController
@RequestMapping("/projects")
@RequiredArgsConstructor
public class ProjectController {

    private final ProjectService projectService;

    /**
     * 创建项目
     */
    @PostMapping
    @PreAuthorize("hasAuthority('ROLE_ADMIN') or hasAuthority('ROLE_RESEARCHER')")
    public ResponseEntity<ProjectDto> createProject(@Valid @RequestBody CreateProjectRequest request) {
        ProjectDto newProject = projectService.createProject(request);
        return ResponseEntity.ok(newProject);
    }

    /**
     * 获取分页和搜索后的项目列表 (主要列表显示)
     * URL: /projects
     */
    @GetMapping
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<PageDto<ProjectDto>> getPaginatedProjects( // 【核心修改】方法名改为 getPaginatedProjects，对应前端API
                                                                     @RequestParam(defaultValue = "1") int pageNum,
                                                                     @RequestParam(defaultValue = "10") int pageSize,
                                                                     @RequestParam(required = false) String projectName, // 匹配前端 searchTag
                                                                     @RequestParam(required = false) String projectCode) { // 匹配前端 searchTag

        // 将参数传递给 Service 层中对应的方法
        PageDto<ProjectDto> projectsPage = projectService.findProjectsByPage(pageNum, pageSize, projectName, projectCode);
        return ResponseEntity.ok(projectsPage);
    }

    /**
     * 获取所有项目列表 (不分页，用于下拉选择等)
     * URL: /projects/all
     */
    @GetMapping("/all") // 【核心修改】新增此路径以提供不分页的所有项目
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<List<ProjectDto>> getAllProjectsNoPagination() {
        return ResponseEntity.ok(projectService.getAllProjectsNoPagination());
    }

    /**
     * 根据ID获取单个项目详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<ProjectDto> getProjectById(@PathVariable Long id) {
        return ResponseEntity.ok(projectService.getProjectById(id));
    }

    /**
     * 获取当前用户参与的项目列表
     * URL: /projects/user
     */
    @GetMapping("/user")
    @PreAuthorize("isAuthenticated()")
    public ResponseEntity<List<ProjectDto>> getUserProjects(Authentication authentication) {
        List<ProjectDto> userProjects = projectService.findProjectsByUsername(authentication.getName());
        return ResponseEntity.ok(userProjects);
    }

    /**
     * 更新项目信息
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('ROLE_ADMIN') or @projectService.canUserModifyProject(#id, authentication.name)")
    public ResponseEntity<ProjectDto> updateProject(@PathVariable Long id, @Valid @RequestBody UpdateProjectRequest request) {
        return ResponseEntity.ok(projectService.updateProject(id, request));
    }

    /**
     * 删除项目
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('ROLE_ADMIN') or @projectService.canUserModifyProject(#id, authentication.name)")
    public ResponseEntity<MessageResponse> deleteProject(@PathVariable Long id) {
        projectService.deleteProject(id);
        return ResponseEntity.ok(new MessageResponse("项目删除成功"));
    }
}